﻿namespace Acme.Marketing.Web.Controllers
{
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web.Mvc;
    using Data;
    using DataAccess;
    using Models;

    public class HomeController : Controller
    {
        private readonly AdvertisementRepository _advertisementRepository;
        private readonly NewspaperRepository _newspaperRepository;

        public HomeController()
        {
            var context = new MarketingContext();
            _newspaperRepository = new NewspaperRepository(context);

            var adContext = new MarketingContext();
            _advertisementRepository = new AdvertisementRepository(adContext);
        }

        public ActionResult Index()
        {
            // retrieve the top 10 most recent of both
            var model = new NewspaperAdsModel();

            using (_newspaperRepository)
            {
                model.Newspapers = _newspaperRepository.GetAll().Include(a => a.Advertisements).ToList();
            }

            using (_advertisementRepository)
            {
                model.Advertisements = _advertisementRepository.GetAll().Include(n => n.Newspapers).ToList();
            }

            return View(model);
        }

        public ActionResult ListNewspaper()
        {
            using (_newspaperRepository)
            {
                List<Newspaper> newspapers = _newspaperRepository.GetAll().Include(a => a.Advertisements).ToList();
                
                return View(newspapers);
            }
        }

        public ActionResult CreateNewspaper()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateNewspaper(Newspaper newspaper)
        {
            using (_newspaperRepository)
            {
                _newspaperRepository.Insert(newspaper);
                _newspaperRepository.SaveChanges();
            }

            return RedirectHome();
        }

        public ActionResult RelateAds(int? id)
        {
            Newspaper paper;

            using (_advertisementRepository)
            {
                paper = _newspaperRepository.Get(id.Value);

                paper.Advertisements = _advertisementRepository.GetAll().ToList();
            }

            return View(paper);
        }

        [HttpPost]
        public ActionResult RelateAds(Newspaper newspaper)
        {
            foreach (var ad in newspaper.Advertisements)
            {
                // save the M:M relationship

            }

            return RedirectHome();
        }

        public ActionResult CreateAdvertisement()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateAdvertisement(Advertisement advertisement)
        {
            using (_advertisementRepository)
            {
                _advertisementRepository.Insert(advertisement);
                _advertisementRepository.SaveChanges();
            }

            return RedirectHome();
        }

        private ActionResult RedirectHome()
        {
            return RedirectToAction("Index", "Home");
        }
    }
}